<template>
  <div class="editor-container">
    <template v-if="editId">
      <!-- 工具栏 -->
      <div class="toolbar">
        <Toolbar :editor="editorRef" :defaultConfig="toolbarConfig" />
        <div class="ai-suggestion-btn" type="primary" plain size="mini" @click="handleAISuggestion">
          <!-- <img src="@/assets/images/write.svg" alt=""> -->
          <svg-icon className="write" iconClass="write"></svg-icon>
          <span class="text">开启续写</span>
        </div>
      </div>

      <div class="editorTitle">
        <el-input v-model="title" placeholder="请输入标题"></el-input>
      </div>
      <!-- 编辑器 -->
      <Editor class="editor" v-model="editorValue" :defaultConfig="editorConfig" @onCreated="handleCreated"
        @onChange="handleSelectionChange" />
      <!-- 自定义悬浮菜单 -->
      <div v-show="showOperationBox" :style="operationBoxStyle" class="operation-box">
        <!-- <div><img src="@/assets/images/icon_xx.png" />续写</div> -->
        <div><img src="@/assets/images/icon_rs.png" />润色</div>
        <!-- <div><img src="@/assets/images/icon_kz.png" />扩展</div> -->
        <div><img src="@/assets/images/icon_kx.png" />扩写</div>
        <div><img src="@/assets/images/icon_gx.png" />细化</div>
      </div>
    </template>
    <div v-else class="noDate">
      <img src="@/assets/images/zzzw.png" alt="">
      无选中的章节内容展示
    </div>
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

export default {
  components: { Editor, Toolbar },
  props: {
    editId: {},
    currentType: {
      type: String,
      default: () => ('')
    },
    // dataObj 包含 { title, content }
    dataObj: {
      type: Object,
      default: () => ({ title: "", content: "", description: "" })
    }
  },
  data () {
    return {
      title: "",
      editorRef: null, // 编辑器实例
      editorValue: '', // 编辑器内容
      toolbarConfig: {
        toolbarKeys: [
          "undo", // 撤销
          "redo", // 重做
          "headerSelect",// 标题
          "|",
          "fontFamily", // 字体
          "bold", // 加粗
          "blockquote", // 引用
          "italic", // 斜体
          "underline", // 下划线
          "through", // 删除线
          "color", // 文字颜色
          "numberedList", // 有序列表
          "bulletedList", // 无序列表
          'left'
        ]
      }, // 工具栏配置
      editorConfig: {
        placeholder: '请输入正文',
        hoverbarKeys: {
          text: {}
        }, // 隐藏默认悬浮菜单
      },
      showOperationBox: false, // 是否显示自定义悬浮菜单
      operationBoxStyle: {
        position: 'absolute',
        top: '0',
        left: '0',
      },
    };
  },
  watch: {
    dataObj: {
      deep: true,
      immediate: false,
      handler () {
        this.initEditorContent();
      }
    }
  },
  methods: {
    //初始化
    initEditorContent () {
      const { title, content, description } = this.dataObj;
      // 将标题作为 <h1> + 正文
      this.title = title;
      if (this.currentType == 1) {
        this.editorContent = `${description}`;
      } else {
        this.editorContent = `${content}`;
      }
    },
    //输出父组件的内容
    onEditorChange ({ html }) {
      // 向父组件输出富文本 HTML（父组件可解析出标题/正文再分开存）
      this.$emit("update-content", html);
    },
    // 编辑器创建时的回调
    handleCreated (editor) {
      this.editorRef = editor;
    },

    // 处理编辑器内容选择事件
    handleSelectionChange () {
      const selection = this.editorRef.getSelectionText();
      if (!selection) {
        // 如果选择区域为空，隐藏悬浮菜单
        this.showOperationBox = false;
      } else {
        // 如果选择了内容，显示悬浮菜单
        this.showOperationBox = true;
        this.positionOperationBox();
      }
    },

    // 定位悬浮菜单
    positionOperationBox () {
      const selection = this.editorRef.getSelectionPosition();
      if (!selection) return;

      // 根据选择区域的位置设置悬浮菜单的位置
      this.operationBoxStyle = {
        position: 'absolute',
        top: selection.top, // 调整菜单位置
        left: selection.left,
      };
      console.log(this.operationBoxStyle)
    },

    // 格式化文本
    formatText (command) {
      if (this.editorRef) {
        this.editorRef.restoreSelection(); // 恢复选择区域
        this.editorRef.executeCommand(command); // 执行命令
      }
    },
    handleAISuggestion () {
      // AI生成标题的逻辑
      // 这里需要根据你的具体需求实现
      console.log("AI生成标题功能尚未实现");
    }
  },
  beforeDestroy () {
    // 组件销毁时销毁编辑器
    if (this.editorRef) {
      this.editorRef.destroy();
    }
  },
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

<style lang="scss" scoped>
.editor-container {
  position: relative;

  .noDate {
    text-align: center;
    padding-top: 350px;
    font-size: 14px;
    color: rgba(0, 1, 27, 0.3);

    img {
      width: 140px;
      display: block;
      margin: 0 auto;
    }
  }

  .toolbar {
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 44px;

    .ai-suggestion-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      background: #165DEB;
      border-radius: 6px;
      width: 88px;
      height: 30px;
      margin-right: 24px;

      .text {
        color: #fff;
        font-weight: 500;
        font-size: 12px;
        color: #FFFFFF;
      }

      .write {
        margin-right: 4px;
      }

    }
  }

  .operation-box {
    position: absolute;

    background: #fff;
    box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.2);
    border-radius: 8px 8px 8px 8px;
    border: 1px solid rgba(0, 0, 0, 0.06);
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 1000;
    transition: opacity 0.3s ease;
    padding: 10px 24px;
    transform: translateY(50px);

    /* 可以根据需要调整操作框的宽高和样式 */
    div {
      cursor: pointer;
      color: #333;
      display: flex;
      align-items: center;
      line-height: 18px;
      font-size: 12px;
      font-weight: bold;
    }

    div+div {
      margin-left: 24px;
    }

    img {
      width: 16px;
      height: 16px;
      margin-right: 4px;
    }
  }

  .editorTitle {
    width: 100%;
    padding: 24px 24px 16px 24px;
    z-index: 999;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);

    ::v-deep .el-input__inner {
      border: none;
      height: 34px;
      line-height: 34px;
      font-size: 24px;
      color: #00011B;
      font-weight: bold;
      padding-left: 0;
    }
  }


}
</style>